package com.lagou.server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
* 编程实现服务器的初始化和关闭
*/

public class ServerInitClose {
    /**
     * 自定义成员变量记录Socket和流对象
     */
    private ServerSocket ss;
    private Socket s;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    private BufferedWriter bw;
    private BufferedReader br;
    private PrintStream ps;

    public ObjectInputStream getOis() {
        return ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    public BufferedWriter getBw() {
        return bw;
    }

    public BufferedReader getBr() {
        return br;
    }

    public PrintStream getPs() {
        return ps;
    }

    /**
     * 自定义成员方法实现服务器的初始化操作
     */
    public void serverInit() throws IOException {
        ss = new ServerSocket(8888);
        System.out.println("等待客户端连接");
        s = ss.accept();
        System.out.println("客户端已连接");

        ois = new ObjectInputStream(s.getInputStream());
        oos = new ObjectOutputStream(s.getOutputStream());
        ps = new PrintStream(s.getOutputStream());
        br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        System.out.println("服务器初始化成功");
    }
    /**
     * 自定义成员方法实现服务器的关闭操作
     */
    public void serverClose() throws IOException {
        bw.close();
        br.close();
        ps.close();
        oos.close();
        ois.close();
        s.close();
        ss.close();
        System.out.println("服务器成功关闭");
    }
}
